home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / crc.lqr / CRC.DOC < prev    next >
Text File  |  1985-06-03  |  7KB  |  136 lines

  1.  
  2.                   Cycliπ Redundanc∙ Checδ (CRC⌐ Functions
  3.  
  4.                                John F. Ratti
  5.                          c/o Computer Power, Inc.
  6.                                 P. O. 2388
  7.                           Jacksonville, FL  32231
  8.                               (904) 350-1400
  9.  
  10.  
  11.  
  12. Thi≤á i≤á thσ .DO├ filσ fo≥ botΦ CRC.├á anΣá CRCCALC.ASM«á Thesσá function≤
  13. providσáá CCIT╘áá o≥áá IB═áá stylσáá CR├áá calculations«áá CRC.├áá contain≤
  14. Lattice/Microsof⌠á "Cóá function≤á t∩ initializσ anΣ terminatσá CR├á calcu-
  15. lations¼ anΣ t∩ perforφ CR├ calculation≤ oε onσ bytσ a⌠ ß time« CRCCALC.AS═
  16. i≤á aε assembl∙ languagσ functioε whicΦ calculate≤ thσ CR├ fo≥ ß numbe≥á oµ
  17. byte≤á oµá datß (u≡ t∩ 64╦ byte≤ a⌠ ß time⌐ severa∞ time≤ faste≥á thaεá thσ
  18. byte-at-a-timσ functioε (CRC_UPDATE⌐ iε CRC.C«á ┴ shor⌠ descriptioε oµá thσ
  19. function≤ follows.
  20.  
  21.  
  22. CRC_CLEA╥á clear≤ thσ CR├ t∩ zero«á Thi≤ functioε shoulΣ bσ calleΣ prio≥ t∩
  23. thσ star⌠ oµ processinτ fo≥ ß blocδ oµ data.
  24.  
  25.  
  26. CRC_UPDAT┼ adjust≤ thσ CR├ fo≥ ß character« I⌠ mus⌠ bσ calleΣ oncσ fo≥ eacΦ
  27. characte≥ t∩ bσ processed iε ß blocδ oµ data.
  28.  
  29.  
  30. CRCCAL├á adjust≤á thσ CR├ fo≥ ß numbe≥ oµ byte≤ oµ datßá a⌠á ßá time«á Thi≤
  31. functioεá i≤á writteε iε assembler¼á anΣ i≤ faste≥ thaε repeateΣá call≤á t∩
  32. CRC_UPDATE« Thσ callinτ sequencσ fo≥ thi≤ functioε i≤ provideΣ below.
  33.  
  34.  
  35. CRC_FINIS╚á terminate≤ thσ CR├ processinτ oµ  ß blocδ oµ data«á I⌠ i≤á useΣ
  36. onl∙á wheε originall∙ calculatinτ thσ CR├ oµ ß block¼á anΣ i≤ calleΣá afte≥
  37. al∞ byte≤ havσ beeε processed« Wheε re-calculatinτ (checking⌐ thσ CR├ fo≥ ß
  38. blocδ oµ data¼á al∞ oµ thσ datß anΣ botΦ byte≤ oµ thσ CR├ mus⌠ bσ processeΣ
  39. b∙ CRC_UPDAT┼ o≥ CRCCALC«á Iµ thσ resultinτ CR├ i≤ zero¼á theε thσ blocδ i≤
  40. good.
  41.  
  42.  
  43. A discussion of CRCCALC:
  44.  
  45. CRCCAL├á calculate≤á thσá CR├ fo≥ ß numbe≥ oµ byte≤ oµá datßá a⌠á once«á I⌠
  46. return≤á aε updateΣ CR├ afte≥ processinτ al∞ thσ bytes«á Thi≤á functioεá i≤
  47. usuall∙á calleΣá onl∙ oncσ fo≥ eacΦ blocδ oµ datß oε whicΦ ß CR├á i≤á beinτ
  48. calculated«á However¼ i⌠ caε bσ calleΣ repeatedly¼ iµ neeΣ be¼ t∩ calculatσ
  49. thσ CR├ wherσ multiplσ record≤ (o≥ othe≥ group≤ oµ data⌐ arσ t∩ bσ includeΣ
  50. iεá ßá singlσ CRC«á Fo≥ example¼á wσ calculatσ thσ CR├ oµ file≤á whicΦá arσ
  51. severa∞á megabyte≤ lonτ b∙ readinτ 32╦ segment≤ oµ thσ filσ anΣá processinτ
  52. eacΦ witΦ CRCCALC« Oncσ thσ entirσ filσ ha≤ beeε processed¼ theε CRC_FINIS╚
  53. i≤á calleΣá t∩ terminatσ thσ processinτ anΣ producσ onσ 16-bi⌠ CR├ fo≥á thσ
  54. entirσ file«á Fastes⌠ speed≤ wil∞ bσ attaineΣ b∙ processinτ a≤ mucΦ datß iε
  55. eacΦ cal∞ t∩ CRCCAL├ a≤ possible.
  56.  
  57. CRCCALC.AS═á i≤ configureΣ fo≥ thσ Lattice/Microsof⌠ "Có compile≥ "L-modeló
  58. (3▓á bi⌠á codσ anΣ datß addresses⌐ callinτ sequence«á Thi≤á caεá bσá easil∙
  59. converteΣá t∩á othe≥á memor∙ model≤ o≥ fo≥ thσ callinτ sequence≤á oµá othe≥
  60. compilers, since only the call/return sequences will differ.
  61.  
  62. Thσ callinτ sequencσ fo≥ eacΦ oµ thσ "Có languagσ function≤ i≤ noteΣ iε thσ
  63. comment≤á fo≥ tha⌠ function«á Sincσ thσ callinτ sequencσ fo≥ CRCCAL├ i≤ no⌠
  64. as obvious, a discussion of that calling sequence follows.
  65. Calling sequence for CRCCALC:
  66.  
  67.  
  68. crc = crccalc(crc, ptr, length);
  69.  
  70. short crc;               /* 16-bit CRC being calculated */
  71. char *ptr;               /* 32-bit pointer to data to be processed */
  72. unsigneΣálength╗á        /* 16-bit lengtΦ oµ datß (▒ to 65535 bytes) */
  73.  
  74. Notσá tha⌠ thσ valuσ iε thσ variablσ "crcó i≤ passeΣ t∩ CRCCALC¼á anΣá tha⌠
  75. thσ updated value is returned and stored back in the same variable.
  76. Example using CRCCALC:
  77.  
  78. short crc;               /* 16-bit CRC being calculated */
  79. char *ptr;               /* 32-bit pointer to data to be processed */
  80. unsigneΣálength╗á        /* 16-bit lengtΦ oµ datß (▒ to 65535 bytes) */
  81. char data[10000];        /* data to be processed */
  82.  
  83. crc = crc_clear();       /* reset crc for next block of data */
  84.  
  85. gets(data);              /* get block of data to be processed */
  86. length = strlen(data);   /* determine the length of the data */
  87. ptr = &data[0];          /* point to the first byte of the data */
  88.  
  89. crc = crccalc(crc, ptr, length);   /* calculate the CRC for this block */
  90.  
  91. crc = crc_finish(crc);   /* terminate crc processing for this block */
  92.  
  93. /* At this point the variable "crc" contains the crc for the string in the  */
  94. /* variable "data". Now we will append the crc to the data, high-order byte */
  95. /* first¼ theε lo≈ orde≥ byte«                                              */
  96.  
  97. data[length▌ ╜ (char⌐ ((crπ ª 0xff00⌐ >╛ 8)╗ /¬ appenΣ crπ high-orde≥ bytσ */
  98. data[lengtΦ ½ 1▌ ╜ (char⌐ (crπ ª 0x00ff)╗    /¬ appenΣ crπ low-orde≥ bytσ */
  99.  
  100. /¬ Thσ variablσ "dataó no≈ contain≤ thσ origina∞ strinτ followeΣ b∙ thσ CRC.*/
  101. /¬ Notσ tha⌠ eithe≥ bytσ oµ thσ CR├ coulΣ bσ zero¼ s∩ "dataó caε n∩ longe≥  */
  102. /* be treated as a string.                                                  */
  103. Example using CRC_UPDATE:
  104.  
  105. short crc;               /* 16-bit CRC being calculated */
  106. unsigneΣálength╗á        /* 16-bit lengtΦ oµ datß (▒ to 65535 bytes) */
  107. char data[10000];        /* data to be processed */
  108. int i;                   /* index for data */
  109.  
  110. crc = crc_clear();       /* reset crc for next block of data */
  111.  
  112. gets(data);              /* get block of data to be processed */
  113. length = strlen(data);   /* determine the length of the data */
  114.  
  115. for (i = 0; i < length; i++)
  116. {    /* for each byte of the string in "data" */
  117.      crπ ╜ crc_update(crc¼á data[i])╗    /¬ calculatσ thσ CR├ fo≥ thi≤ bytσ */
  118. }    /* end of for loop */
  119.  
  120. crc = crc_finish(crc);   /* terminate crc processing for this block */
  121.  
  122. /* At this point the variable "crc" contains the crc for the string in the  */
  123. /* variable "data". Now we will append the crc to the data, high-order byte */
  124. /* first¼ theε lo≈ orde≥ byte«                                              */
  125.  
  126. data[length▌ ╜ (char⌐ ((crπ ª 0xff00⌐ >╛ 8)╗ /¬ appenΣ crπ high-orde≥ bytσ */
  127. data[lengtΦ ½ 1▌ ╜ (char⌐ (crπ ª 0x00ff)╗    /¬ appenΣ crπ low-orde≥ bytσ */
  128.  
  129. /¬ Thσ variablσ "dataó no≈ contain≤ thσ origina∞ strinτ followeΣ b∙ thσ CRC.*/
  130. /¬ Notσ tha⌠ eithe≥ bytσ oµ thσ CR├ coulΣ bσ zero¼ s∩ "dataó caε n∩ longe≥  */
  131. /* be treated as a string.                                                  */
  132.  
  133.  
  134.  
  135. /***************************** end of CRC.DOC ********************************/
  136.